package com.hk.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangzf
 * @description  计算工具
 * @since wangzf01 17:12
 **/
public class NumberUtil {

    /**
     * 计算BigDecimal和
     * @param dataArray
     * @return
     */
    public static BigDecimal addBigDecimals(BigDecimal... dataArray){
        if(dataArray == null){
            return null;
        }
        BigDecimal data = new BigDecimal(0);
        for(BigDecimal item : dataArray){
            if(item != null){
                data = data.add(item);
            }
        }
        return data;
    }

    /**
     * 字符串转数字类型
     * @param val
     * @return
     */
    public static BigDecimal strToBigDecimal(String val){
        if(val == null || !val.matches("\\d+(\\.\\d+)?")){
            return null;
        }
        return new BigDecimal(val);
    }

    /**
     * 计算Integer和
     * @param dataArray
     * @return
     */
    public static Integer addIntegers(Integer... dataArray){
        if(dataArray == null){
            return null;
        }
        Integer data = 0;
        for(Integer item : dataArray){
            if(item != null){
                data += item;
            }
        }
        return data;
    }

    /**
     * 计算Long和
     * @param dataArray
     * @return
     */
    public static Long addLongs(Long... dataArray){
        if(dataArray == null){
            return null;
        }
        Long data = 0L;
        for(Long item : dataArray){
            if(item != null){
                data += item;
            }
        }
        return data;
    }

    /**
     * 判断一个数是否为奇数，奇数为true，偶数为false
     * @param a
     * @return
     */
    public static boolean isOdd(int a){
        if((a&1) == 1){//是奇数
            return true;
        }
        return false;
    }

    /**
     * 字符型数值转为Long
     */
    public static Long toLong(String str){
        if(str != null && str.matches("^[1-9][0-9]+")) {
            return Long.parseLong(str);
        }
        return null;
    }

    /**
     * Long转为String
     */
    public static String toString(Long val){
        if(val == null){
            return null;
        }
        return String.valueOf(val);
    }

    /**
     * 计算同比/环比 %
     */
    public static BigDecimal computeCurrToLastRatio(BigDecimal curr, BigDecimal last){
        if(curr == null){
            return new BigDecimal(0);
        }
        if(last == null || last.compareTo(new BigDecimal(0)) == 0){
            return null;
        }
        return curr.subtract(last).multiply(new BigDecimal(100)).divide(last, 2, RoundingMode.HALF_DOWN);
    }

    /**
     * 计算同比/环比%（单位转换）
     * @param curr
     * @param last
     * @param unit
     * @return
     */
    public static BigDecimal computeCurrToLastRatio(BigDecimal curr, BigDecimal last, BigDecimal unit){
        if(curr != null){
            curr = curr.divide(unit);
        }
        return computeCurrToLastRatio(curr, last);
    }

    /**
     * 计算比率 %
     */
    public static BigDecimal computeRatio(BigDecimal divider, BigDecimal dividend){
        if(divider == null){
            return new BigDecimal(0);
        }
        if(dividend == null || dividend.compareTo(new BigDecimal(0)) == 0){
            return null;
        }
        return divider.multiply(new BigDecimal(100)).divide(dividend, 2, RoundingMode.HALF_DOWN);
    }

    /**
     * 行序号处理
     * @param hxh
     * @return
     */
    public static Integer convertHxh(Long hxh) {
        String hxhstr = String.valueOf(hxh);
        if (hxhstr.length() >=3) {
            hxhstr = hxhstr.substring(1,hxhstr.length());
            return Integer.parseInt(hxhstr);
        }
        return 0;
    }

    /**
     * 从字符串中提取数字
     *
     * @param str
     * @return
     */
    public static String getNumberFromString(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("");
    }
}
